<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="页面码" prop="senceCode">
        <el-select v-model="queryParams.senceCode" placeholder="请选择页面码" clearable>
          <el-option
            v-for="dict in dict.type.content_page"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="权重" prop="weight">
        <el-input
          v-model="queryParams.weight"
          placeholder="请输入权重"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="内容数量" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入可看内容数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="模块名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模块名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentCategoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="昵称" align="center" prop="nickName"/>

      <!--      <el-table-column label="${comment}" align="center" prop="id" />-->
      <el-table-column label="头像" align="center" prop="avatar">
        <template slot-scope="scope">
         <image-preview :src="scope.row.avatar" :width="50" :height="50" />
        </template>
        
      </el-table-column>

      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <span>{{ scope.row.sex == '0'? '男':scope.row.sex == '1'?'女':scope.row.sex == '2'?'未知':'保密' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="简介" align="center" prop="introduction" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改内容模块对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-upload ref="uploadComp" class="upload-demo" :action="upUrl" name="multipartFile" :on-change="onChangeFile" v-model="form.avatar"
          :on-preview="handlePreview" :on-remove="handleRemove" :before-remove="beforeRemove" :limit="2"
          :on-exceed="handleExceed" :on-success="hanldeSuccess" :headers="headers" :show-file-list="true"
          :auto-upload="true">
          <el-button type="warning" plain icon="el-icon-upload" size="mini" @click="handleImportMusic"
           >虚拟头像</el-button>
        </el-upload>
        </el-form-item>
 
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio :label="'0'">男</el-radio>
            <el-radio :label="'1'">女</el-radio>
            <el-radio :label="'2'">未知</el-radio>
            <el-radio :label="'3'">保密</el-radio>
          </el-radio-group>

        </el-form-item>
        <el-form-item label="简介" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入简介" />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getVirtualUser,upVirtualUser,addVirtualUser,delVirtualUser } from "../../../api/system/musicinfo";

export default {
  name: "ContentCategory",
  dicts: ['content_page'],
  data() {
    return {
      
      upUrl: process.env.VUE_APP_BASE_API + '/v1/app/public/upload',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 虚拟用户表格数据
      contentCategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        type:'02'
      },
      // 表单参数
      form: {
        userId:null,
avatar:null,
nickName:null,
sex:null,
introduction:null,
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },



  methods: {
    /** 查询内容模块列表 */
    getList() {
      this.loading = true;
      if(this.queryParams.type == "02"){
        getVirtualUser(this.queryParams).then(response => {
        this.contentCategoryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      }
      
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$refs.uploadComp.clearFiles()
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        avatar: null,
        senceCode: null,
        nickName: null,
        sex: null,
        introduction: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加虚拟用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

        this.form.userId = row.userId;
  this.form.avatar = row.avatar;
  this.form.nickName = row.nickName;
  this.form.sex = row.sex;
  this.form.introduction = row.introduction;
        this.open = true;
        this.title = "修改虚拟用户";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            upVirtualUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVirtualUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.userId || this.ids;

      this.$modal.confirm('是否确认删除内容模块编号为"' + ids + '"的数据项？').then(function() {
        return delVirtualUser({list:[ids]});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download('system/contentCategory/export', {
    //     ...this.queryParams
    //   }, `contentCategory_${new Date().getTime()}.xlsx`)
    // }

    //   // 上传成功
    // imgSuccess(res, file, fileList) {
    //   console.log('====================================');
    //   console.log(res, file, fileList);
    //   console.log('====================================');
    //   // this.fileList = fileList;
    //   //这里我是用一个fileList数组存取，当保存的时候进行图片路径处理
    // },
    // // 上传失败
    // imgError(res) {
    //   this.$message({
    //     type: "error",
    //     message: "附件上传失败",
    //   });
    // },

    // // 删除图片
    // imgRemove(file, fileList) {
    //   // this.fileList = fileList;
    // },

        /**导入曲目**/
        handleImportMusic() {
      
      console.log("~~@@@@@@~~~~~")
    },
    handleRemove(multipartFile, multipartFileList) {
      console.log(multipartFile, multipartFileList);
    },
    handlePreview(multipartFile) {
      console.log(multipartFile);
    },
    handleExceed(multipartFiles, multipartFileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${multipartFiles.length} 个文件，共选择了 ${multipartFiles.length + multipartFileList.length} 个文件`);
    },
    beforeRemove(multipartFile, multipartFileList) {
      return this.$confirm(`确定移除 ${multipartFile.name}？`);
    },
    hanldeSuccess(res,multipartFile, multipartFileList) {
      if(res.code == 200){
        console.log('====================================');
        console.log(res);
        console.log('====================================');
        this.form.avatar = res.data.url
      }
      this.getList()
    },
  }
};
</script>
